package com.fxwater.admin.controller.biz;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fxwater.biz.domain.FxwAlarmRecordInfo;
import com.fxwater.biz.domain.FxwAlermConfigInfo;
import com.fxwater.biz.service.IAlermService;
import com.fxwater.common.annotation.Log;
import com.fxwater.common.core.controller.BaseController;
import com.fxwater.common.core.domain.AjaxResult;
import com.fxwater.common.core.page.TableDataInfo;
import com.fxwater.common.enums.BusinessType;
import com.fxwater.common.utils.poi.ExcelUtil;
import com.fxwater.framework.util.ShiroUtils;
import com.fxwater.system.domain.SysUser;

/**
 * 泵站告警管理
 * @author tx
 *
 */
@Controller
@RequestMapping("/biz/alerm")
public class AlermController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(AlermController.class);
	 
	private String prefix = "biz/alerm";
	
	@Autowired
	private IAlermService alermService;
	
	
	/**
	 * 修改息状态
	 * @return
	 */
	@RequestMapping("/changeStatus")
	@RequiresPermissions("biz:alerm:setEdit")
	@ResponseBody
	public AjaxResult changeStatus(String id,String status) {
		FxwAlermConfigInfo alermConfigInfo = new FxwAlermConfigInfo();
		alermConfigInfo.setId(Long.parseLong(id));
		alermConfigInfo.setIsValid(status);
		return toAjax(alermService.editAlermConfigInfo(alermConfigInfo));
	}
	
	/**
	 * 跳转页面
	 * 
	 * @return
	 */
	@RequiresPermissions("biz:alerm:setView")
	@GetMapping("/config")
	public String alertConfig() {
		return prefix + "/config";
	}
	
	/**
	 * 跳转到告警数据列表页面
	 * @return
	 */
	@RequiresPermissions("biz:alert:recordView")
	@GetMapping("/record")
	public String record() {
		return prefix + "/record";
	}
	
	
	/**
	 * 查询泵站告警记录列表
	 * @param unitInfo
	 * @return
	 */
	@RequiresPermissions("biz:alert:recordList")
	@PostMapping("/record/recordList")
	@ResponseBody
	public TableDataInfo recordList(FxwAlarmRecordInfo alrmRecordInfo) {
		startPage();
		List<FxwAlarmRecordInfo> list = alermService.selectAlarmRecordInfo(alrmRecordInfo);
		return getDataTable(list);
	}
	
	
	
	@Log(title = "告警记录管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("biz:alert:recordExport")
    @PostMapping("/record/recordExport")
    @ResponseBody
    public AjaxResult export(FxwAlarmRecordInfo alrmRecordInfo)
    {
        List<FxwAlarmRecordInfo> list = alermService.selectAlarmRecordInfo(alrmRecordInfo);
        ExcelUtil<FxwAlarmRecordInfo> util = new ExcelUtil<FxwAlarmRecordInfo>(FxwAlarmRecordInfo.class);
        return util.exportExcel(list, "告警记录数据");
    }
	
	
	/**
	  * 删除
	  * @return
	  */
	 @RequestMapping("/config/remove")
	 @ResponseBody
	 @RequiresPermissions("biz:alerm:setRemove")
	 @Log(title = "泵站告警删除", businessType = BusinessType.DELETE)
	 public AjaxResult remove(String ids) {
		 return toAjax(alermService.removeAlermConfigInfo(ids));
	 }
	
	
	/**
	 * 查询泵站告警设置列表
	 * @param unitInfo
	 * @return
	 */
	@RequiresPermissions("biz:alerm:setList")
	@PostMapping("/config/list")
	@ResponseBody
	public TableDataInfo list(FxwAlermConfigInfo alertConfig) {
		startPage();
		List<FxwAlermConfigInfo> list = alermService.selectAlermConfigInfoList(alertConfig);
		return getDataTable(list);
	}
	
	/**
	 * 查询泵站告警设置列表
	 * @param unitInfo
	 * @return
	 */
	@RequiresPermissions("biz:alerm:setList")
	@PostMapping("/config/detailList")
	@ResponseBody
	public TableDataInfo detailList(String stationId) {
		FxwAlermConfigInfo alertConfig = new FxwAlermConfigInfo();
		alertConfig.setStationId(Long.parseLong(stationId));
		startPage();
		List<FxwAlermConfigInfo> list = alermService.selectAlermConfigInfoList(alertConfig);
		return getDataTable(list);
	}
	
	/**
	 * 增加告警设置
	 * 
	 * @param mmap
	 * @return
	 */
	@GetMapping("/config/add")
	public String add(ModelMap mmap,String stationId,String stationName) {
		if(StringUtils.isEmpty(stationId)) {
			return prefix + "/configAdd";
		}else {
			mmap.put("stationId", stationId);
			mmap.put("stationName", stationName);
			return prefix + "/configDetailAdd";
		}
		
	}


	/**
	 * 添加泵站
	 * 
	 * @param user
	 * @return
	 */
	@RequiresPermissions("biz:alerm:setAdd")
	@Log(title = "告警设置", businessType = BusinessType.INSERT)
	@PostMapping("/config/add")
	@ResponseBody
	public AjaxResult addSave(FxwAlermConfigInfo alermConfigInfo) {
		alermConfigInfo.setCreateBy(ShiroUtils.getLoginName());//
		return toAjax(alermService.addAlermConfigInfo(alermConfigInfo));
	}
	
	

	/**
	 * 跳转修改页面
	 * 
	 * @param mmap
	 * @return
	 */
	@GetMapping("/config/edit/{id}")
	public String edit(ModelMap mmap, @PathVariable("id") String id) {
		FxwAlermConfigInfo alermConfigInfo = new FxwAlermConfigInfo();
		alermConfigInfo.setId(Long.parseLong(id));
		alermConfigInfo = alermService.selectAlermConfigInfoList(alermConfigInfo).get(0);
		mmap.addAttribute("alermConfigInfo", alermConfigInfo);
		return prefix + "/configEdit";
	}
	
	/**
	 * 保存泵站修改信息
	 * 
	 * @return
	 */
	@RequiresPermissions("biz:alerm:setEdit")
	@Log(title = "泵站告警编辑", businessType = BusinessType.UPDATE)
	@PostMapping("/config/edit")
	@ResponseBody
	public AjaxResult editSave(FxwAlermConfigInfo alermConfigInfo) {

		return toAjax(alermService.editAlermConfigInfo(alermConfigInfo));
	}
	
	
}
